package com.lanlian.commonlib.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.lanlian.commonlib.bean.db.DBUser;
import com.lanlian.commonlib.greendao.DBMarketSDao;
import com.lanlian.commonlib.greendao.DBMarketSerchDao;
import com.lanlian.commonlib.greendao.DBMessageDao;
import com.lanlian.commonlib.greendao.DBStyleDao;
import com.lanlian.commonlib.greendao.DBUserDao;
import com.lanlian.commonlib.greendao.DaoMaster;

import org.greenrobot.greendao.database.Database;

import static com.lanlian.commonlib.greendao.DaoMaster.dropAllTables;


public class DBOpenHelper extends DaoMaster.OpenHelper {

    private String name;

    public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
        Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
        if (newVersion > oldVersion) {
            // 升级、数据库迁移操作
            MigrationHelper.getInstance().migrate(db, DBUserDao.class, DBMarketSDao.class,
                    DBMarketSerchDao.class, DBMessageDao.class, DBStyleDao.class);
        } else {
            // 默认操作
            dropAllTables(db, true);
            onCreate(db);
        }
    }
}
